package com.miniprogram.admin.advice;

import cn.hutool.json.JSONUtil;
//import com.miniprogram.admin.utils.ShiroUtils;
//import com.miniprogram.bean.vo.admin.AdminUserVo;
import com.miniprogram.admin.utils.ShiroUtils;
import com.miniprogram.bean.vo.admin.AdminUserVo;
import com.miniprogram.common.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * 系统日志，切面处理类
 *
 * @author lin
 * @since 2022年06月21日
 */
@Aspect
@Component
@Slf4j
public class SysLogAspect {

    /**
     * 切点
     */
    @Pointcut("execution(* com.miniprogram.admin.controller.*.*(..)))")
    public void logPointCut() {

    }

    /**
     * 前置通知
     *
     * @param joinPoint 连接点
     */
    @Before("logPointCut()")
    public void saveSysLog(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        //请求的类
        String className = joinPoint.getTarget().getClass().getSimpleName();
        // 方法名
        String methodName = signature.getName();

        //请求的参数
        Object[] args = joinPoint.getArgs();
        //获取request
        HttpServletRequest request = HttpUtils.getHttpServletRequest();
        //设置IP地址
        String ip = HttpUtils.getIpAddr(request);

        //用户名
        AdminUserVo sysUserEntity = ShiroUtils.getUserEntity();
        String username = "";
        if (null != sysUserEntity) {
            username = ShiroUtils.getUserEntity().getUsername();
        }
        log.info("正在调用接口类为{}，接口为{}，调用人为{}，ip地址为{}，调用参数为{}", className, methodName, username, ip, JSONUtil.toJsonStr(args));
    }

}
